package com.yjps.common.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求设置跨域请求头Access-Control-Allow-Origin = *
 * Created by jessy on 2015/9/2.
 */
public class MyAdviceFilter implements Filter {
    Logger log = LoggerFactory.getLogger(MyAdviceFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            HttpServletResponse resp = (HttpServletResponse) response;
            resp.setHeader("Access-Control-Allow-Origin", "*");
            log.debug("set header success ! ");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
